package com.xlf.common.controller;

import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.aop.ClearInterceptor;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.xlf.cache.CatalogList;
import com.xlf.cache.Option;
import com.xlf.common.interceptor.FactoryInterceptor;
import com.xlf.common.model.Factory;
import com.xlf.common.model.Order;
import com.xlf.common.model.OrderCatalog;
import com.xlf.common.model.OrderItem;
import com.xlf.common.model.Product;
import com.xlf.web.SessionKey;

@Before(FactoryInterceptor.class)
public class FactoryController extends Controller {

	@ClearInterceptor
	public void login() {
		renderJsp("login.jsp");
	}

	public void order() {
		renderJsp("list_order.jsp");
	}

	public void index() {
		renderJsp("list_order.jsp");
	}

	public void loginForm() {
		String username = getPara("username");
		String password = getPara("pwd");
		try {
			Factory factory = Factory.dao.exeFindByName(username, password);
			if (factory != null) {
				int f_id = factory.get("f_id");
				// by zyk 把厂家管理员的fid放入session中
				setSessionAttr("f_id", f_id);
				saveUser(factory);
				search();
			} else {
				renderJsp("login.jsp");
			}
		} catch (Exception e) {
			String notice = "登陆失败，请检查用户名和密码";
			setAttr("notice", notice);
			renderJsp("login.jsp");
		}

	}

	public void logout() {
		removeSessionAttr(SessionKey.USER_TYPE);
		removeSessionAttr(SessionKey.USER);

		String from = getPara("from");
		if (from.equals("factory")) {
			forwardAction("/factory/login");
		} else if (from.equals("factory")) {
			forwardAction("/factory/login");
		}

	}

	private void saveUser(Factory user) {
		setSessionAttr(SessionKey.USER, user);
		setSessionAttr(SessionKey.USER_TYPE, user.getInt("type"));
	}

	public void search() {
		String type = getPara("type");
		String status = getPara("status");
		if(status == null || status.equals("")) {
			status = "-1";
		}
		
		setAttr("type", type);
		setAttr("status", status);
		
		Integer fId = getSessionAttr("f_id");
		Page<Product> pageRecord = Product.dao.exeSearchSql(getParaToInt("page", 1), 20, null, type, null, status, (String) getSessionAttr(SessionKey.PLAN_TYPE_STR), fId);
		setAttr("pageRecord", pageRecord);

		render("product.jsp");
	}

	public void ordersearch() {
		String o_id = getPara("o_id");
		String createTime = getPara("createtime");
		setAttr("o_id", o_id);
		setAttr("createTime", createTime);
		
		String status = "1"; // 检索已审核状态订单
		int f_id = getSessionAttr("f_id");
		Page<Order> pageRecord = Order.dao.exeOrderSearch(getParaToInt("page", 1), 20, o_id, createTime, status, f_id);
		setAttr("pageRecord", pageRecord);
		setAttr("courseType", "0");

		render("list_order.jsp");
	}

	public void orderconfirm() {
		String o_id = getPara("o_id");
		int f_id = getSessionAttr("f_id");
		Page<OrderItem> pageRecord = OrderItem.dao.exeOrderItemConfirm(getParaToInt("page", 1), 30, o_id, "", "-1", f_id);
		setAttr("orderitem", pageRecord.getList().get(0));
		setAttr("pageRecord", pageRecord);
		// by zyk 补充说明是创建订单时设计师填写的，字段保存在 OrderCatalog 表里，catalogRemark,直接显示即可
		Integer c_id = pageRecord.getList().get(0).getInt("c_id");
		String oid = pageRecord.getList().get(0).getStr("o_id");
		OrderCatalog ordercatalog = OrderCatalog.dao.exeFindById(oid, c_id);
		String catalogRemark = ordercatalog.get("catalogRemark");
		setAttr("buchong", catalogRemark);
		render("form_order.jsp");
	}

	public void ordersave() {
		String[] id = getParaValues("o.i_id");
		String[] scaleTime = getParaValues("scaleTime");
		String[] deliverTime = getParaValues("deliverTime");
		String batch = getPara("batch", "off");

		if (id != null) {
			for (int i = 0; i < id.length; i++) {
				OrderItem orderitem = OrderItem.dao.findById(id[i]);
				if (batch.equals("on")) {
					// 开启批量确认
					String pscaleTime = getPara("pscaleTime");
					String pdeliverTime = getPara("pdeliverTime");
					orderitem.set("scaleTime", pscaleTime);
					orderitem.set("deliverTime", pdeliverTime);
				} else {
					orderitem.set("scaleTime", scaleTime[i]);
					orderitem.set("deliverTime", deliverTime[i]);
				}

				orderitem.update();
			}

		}

		ordersearch();
	}

	public void product() {
		String name = getPara("name");
		// int f_id = getSessionAttr("f_id");
		Page<Factory> pageRecord = Factory.dao.exeProductByFid(getParaToInt("page", 1), 10, name, 0);
		setAttr("pageRecord", pageRecord);
		render("index.jsp");
	}

	public void save() {
		Factory factory = getModel(Factory.class, "factory");

		if (factory.get("f_id") == null) {
			// 添加厂家状态默认生效
			factory.save();
		} else if (factory.getInt("f_id") > 0) {
			factory.update();
		}
		product();
	}

	public void productsave() {
		// Factory factory = getModel(Factory.class, "factory");
		Product product = getModel(Product.class, "product");
		product.update();
		search();
	}

	public void form() {
		Integer id = getParaToInt("id");
		if (id != null && id > 0) {
			String catalog = getPara("catalogsel");
			setAttr("factory", Factory.dao.findById(id));
			setAttr("catalogsel", catalog);
			List<Option> catalogList = CatalogList.getList();
			setAttr("catalogList", catalogList);

		}
		renderJsp("form.jsp");
	}

	public void productform() {
		Integer id = getParaToInt("id");
		if (id != null && id > 0) {
			Product product = Product.dao.findById(id);

			setAttr("product", product);
		}
		renderJsp("productform.jsp");
	}

	public void delete() {
		Integer id = getParaToInt("id");
		String notice = "删除厂家操作失败";
		if (id != null && id > 0) {
			boolean flag = Factory.dao.deleteById(id);
			if (flag) {
				notice = "删除厂家操作成功";
			}
		}
		setAttr("notice", notice);
		product();
	}

	public void addNew() {
		List<Option> catalogList = CatalogList.getList();
		setAttr("catalogList", catalogList);

		renderJsp("form.jsp");
	}

	/**
	 * 供前台使用Ajax根据主材类型查询厂家列表
	 */
	public void getSelectList() {
		List<Factory> factoryList = Factory.dao.find("select f_id, name from factory where c_id=?", getParaToInt("c_id"));
		renderJson(factoryList);
	}

}
